package com.zhenyi.tuangou.index;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheName;
import com.zhenyi.common.model.TgProduct;

public class ProductController extends TGController {

	/**
	 * 商品首页
	 */
	// @Before(CacheInterceptor.class)
	// @CacheName("60time")
	@Clear
	public void index() {
		Integer type = getParaToInt("type");// 默认全部类型
		Long shopId = getParaToLong("shopId", (long) 1802);
		SqlPara sqlPara;
		if (type != null) {
			Kv cond = Kv.by("type", type);
			sqlPara = Db.getSqlPara("findProductByType", cond);
		} else {
			sqlPara = Db.getSqlPara("findProductByType");
		}
		// Page<Record> list = Db.paginate(pageNumber, PAGE_SIZE, sqlPara);//
		// 商品列表
		List<Record> list = Db.find(sqlPara);

		Kv cond1 = Kv.by("shopId", shopId);
		SqlPara sqlPara2 = Db.getSqlPara("findProductType", cond1);
		List<Record> typeList = Db.find(sqlPara2);
		setAttr("typeList", typeList);
		setAttr("items", list);
		// render("/res/index.html");
		renderJson(typeList);
	}

	/**
	 * 商品详情
	 */
	public void getProductById() {
		Long productId = getParaToLong("id");// 商品id
		if (productId == null) {// 判空
			renderFailure();
			return;
		}
		TgProduct product = getProduc(productId);
		setAttr("product", product);// 商品信息

		String[] images = getImages(product.getImages());
		setAttr("images", images);// 轮播图url

		Kv cond = Kv.by("productId", productId);
		SqlPara sql = Db.getSqlPara("findGroupProduct", cond);
		List<Record> list = Db.find(sql);
		setAttr("groups", list);// 阶梯团列表

		Kv cond1 = Kv.by("productId", productId);
		SqlPara sqlPara = Db.getSqlPara("findProductItem", cond1);
		List<Record> list1 = Db.find(sqlPara);// 产品规格列表
		setAttr("productItem", list1);

		setAttr("user", getUser());
		// render("/res/add.html");
		render("/res/productInfo.html");
	}

}
